/**
 * 
 */
package com.detroitchow2;

import java.util.ArrayList;

import com.detroitchow2.layout.StoresArrayAdapter;
import com.detroitchow2.util.LogUtil;
import com.detroitchow2.vo.StoreVO;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;

/**
 * @author dave
 *
 */
public class SearchActivity extends Activity implements OnItemClickListener
{

  private StoreService storeService= null;
  private AutoCompleteTextView textView;
  private ArrayList<String> autoCompleteArray=null;
  private ListView storesListView=null;
  private ArrayAdapter<StoreVO> storesArrayAdapter=null;
  private ArrayList<StoreVO> stores=null;
  private Intent myIntent;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    storesListView = (ListView) this.findViewById(R.id.lvStores);
    storeService = ((StoreService)getApplicationContext());
    if(storeService.isStoresAlive()) {
      
      autoCompleteArray = storeService.getAutoCompleteArray();
      LogUtil.log("Search onCreate", "" + autoCompleteArray.size());
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.search_item, autoCompleteArray);
      textView.setAdapter(adapter);
      textView.setOnKeyListener(keyListener);
      //textView.setOnItemSelectedListener(selectedItemListener);
      textView.setOnItemClickListener(itemClickListener);
      //textView.setO
      
    } else {
      myIntent = new Intent(this, MainActivity.class);
     startActivityForResult(myIntent, 0);
    }
  }

  private void executeSearch(String s) 
  {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);
    storesListView.removeAllViewsInLayout();
    stores = storeService.getSearch(s);
    storesArrayAdapter = new StoresArrayAdapter(this, R.layout.directoryitem, R.id.txtstoretitle, stores);
    if(storesArrayAdapter != null){
      storesListView.setAdapter(storesArrayAdapter);
      storesListView.setTextFilterEnabled(true);
      storesListView.setOnItemClickListener(this);
      
    }
  }
  
  public boolean onCreateOptionsMenu(Menu menu)
  {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    ActionBar actionBar = getActionBar(); // || getActionBar();
    actionBar.setIcon(getResources().getDrawable(R.drawable.action_bar_icon));
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setTitle("");
    return true;
    
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) 
  {
    Intent intent;
    
    switch (item.getItemId()) {
    
        case android.R.id.home:
          intent = new Intent(this, MainActivity.class);
          startActivityForResult(intent, 0);
          return false;
      
        case R.id.refresh:
          intent = new Intent(this, MainActivity.class);
          intent.putExtra(AppIntent.REFRESH, true);
          startActivityForResult(intent, 0);
          return false;
          
        default:
            return super.onOptionsItemSelected(item);
    }
  }

  
  private OnKeyListener keyListener = new OnKeyListener() {
    
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
      if (event.getAction() == KeyEvent.ACTION_DOWN)
      {
          switch (keyCode)
          {
              case KeyEvent.KEYCODE_DPAD_CENTER:
              case KeyEvent.KEYCODE_ENTER:
                 LogUtil.log("keyListener", "Picked Up enter Key search text: " + textView.getEditableText());
                
                 executeSearch(textView.getEditableText().toString());
              default:
                  break;
          }
      }
      return false;
    }

    
    
  };
  
  private OnItemClickListener itemClickListener = new OnItemClickListener() {

    public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
      // TODO Auto-generated method stub
      String selection =  (String) arg0.getItemAtPosition(position);
      LogUtil.log("Search", "onItemClicked position: " + position + " selectedObject: " + selection);
      textView.setText(selection);
      executeSearch(selection);
    }
    
  };
  
 
  @Override
  public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) 
  {
    // TODO Auto-generated method stub
    LogUtil.log("Search", "onItemClick index: " + index);
    int selectedStoreId =  stores.get(index).mapGroupId ;
    
    LogUtil.log("Search", "onItemClick selectedStoreId: " + selectedStoreId);
    int realStoreId =  stores.get(index).storeId ;
    LogUtil.log("Search", "onItemClick realStoreId: " + realStoreId);
    Intent myIntent = new Intent(this, MapScreen.class);
    myIntent.putExtra(AppIntent.MAP_INTENT, AppIntent.SELECT_STORE);
    myIntent.putExtra(StoreService.SELECTED_STORE_KEY, selectedStoreId);
    myIntent.putExtra(StoreService.SELECTED_STORE_OVERRIDE, realStoreId);
    startActivityForResult(myIntent, 0);
  }

}
